package com.lwm.mybatis.custom.proxy;

import com.lwm.mybatis.custom.cfg.Mapper;
import com.lwm.mybatis.custom.utils.Executor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.Map;

/**
 * @author liweimin
 * @date 2021/6/15 18:12
 * @since
 */
public class MapperProxy implements InvocationHandler {

    private Map<String, Mapper> mappers;
    private Connection connection;

    public MapperProxy(Map<String, Mapper> mappers, Connection connection) {
        this.mappers = mappers;
        this.connection = connection;
    }

    public MapperProxy() {
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //1. 方法名
        String methodName = method.getName();
        String className = method.getDeclaringClass().getName();
        String key = className + "." + methodName;
        Mapper mapper = mappers.get(key);
        if (mapper == null) {
            throw new IllegalArgumentException("传入参数有误！！！");
        }
        return new Executor().selectList(mapper, connection);
    }
}
